/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes;

import com.android1.amarena2d.animation.Animation;
import com.android1.amarena2d.animation.Animations;
import com.android1.amarena2d.commons.Callback;
import com.android1.amarena2d.input.TouchListenerAdapter;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.SimpleLabel;
import com.android1.amarena2d.nodes.behavior.ActionTarget;
import com.android1.amarena2d.nodes.sprites.Sprite;
import com.badlogic.gdx.graphics.Color;
import org.amarena2d.examples.Example;

@Example(name = "Sprite Animation", category = "Sprite")
public class SpriteAnimationExampleScene extends Scene {

    Animation explodeAnimation;
    Animation fallAnimation;
    Animation batAnimation;
    Sprite andou;
    Sprite bat;

    public SpriteAnimationExampleScene() {

        engine.getRenderer().setClearColor(Color.WHITE);

        //bad example: creating an animation from different images (TextureAtlas should be favored)
        explodeAnimation = Animations.$(0.1F)
                .addFrame("anim/explode/andou_explode00.png")
                .addFrame("anim/explode/andou_explode01.png")
                .addFrame("anim/explode/andou_explode02.png")
                .addFrame("anim/explode/andou_explode03.png")
                .addFrame("anim/explode/andou_explode04.png")
                .addFrame("anim/explode/andou_explode05.png")
                .addFrame("anim/explode/andou_explode06.png")
                .addFrame("anim/explode/andou_explode07.png")
                .addFrame("anim/explode/andou_explode08.png")
                .addFrame("anim/explode/andou_explode09.png")
                .addFrame("anim/explode/andou_explode10.png")
                .addFrame("anim/explode/andou_explode11.png")
                .addFrame("anim/explode/andou_explode12.png").create();


        batAnimation = Animations.$(0.2F)
                .addFrame("anim/bat/enemy_bat01.png")
                .addFrame("anim/bat/enemy_bat02.png")
                .addFrame("anim/bat/enemy_bat03.png")
                .addFrame("anim/bat/enemy_bat04.png").create();

        //The first frame is the default texture.
        //The Animation
        andou = new Sprite(explodeAnimation, 150, 150);
        andou.touch().makeTouchable(new TouchListenerAdapter() {
            @Override
            public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
                andou.animation().selectDefault().play();
                return true;
            }
        });


        bat = new Sprite(batAnimation, 150, 150);


        add(andou, bat);

        add(new SimpleLabel("(Touch Andou to let him explode. Touch the screen to fly the bat.)", 10, 20, Color.BLACK));

        touch().makeTouchable(this);

    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        bat.move().to(x, y, 1).setOnFinished(batFinishedToMove);
        bat.animation().selectDefault().play();
        return true;
    }

    Callback<ActionTarget> batFinishedToMove = new Callback<ActionTarget>() {
        @Override
        public void on(ActionTarget element) {
            bat.animation().stop();
        }
    };

    @Override
    protected void onInit() {
        //Animaions that are no default animation, will not be init during Scene.init() as they have no association with the scene.
        //That has to be done explicite.
//        fallAnimation.init();
    }
}